package com.dwei;

import java.io.File;
import java.io.FileNotFoundException;
import java.io.FileOutputStream;
import java.io.IOException;
import java.io.OutputStreamWriter;
import java.io.Writer;
import java.util.HashMap;
import java.util.Map;

import org.apache.commons.lang.StringUtils;
import org.dom4j.Document;
import org.dom4j.DocumentException;
import org.dom4j.io.SAXReader;

import freemarker.template.Template;
import freemarker.template.TemplateException;

public class Automation {
	private FreemarkerUtil freemarkerUtil;
	private MyConfiguration myConfiguration;
	
	/**
	 * 构造函数
	 * @param pathFTL 模板所在路径
	 * @param pathCONF 配置文件所在路径
	 * @param nameCONF 配置文件名
	 */
	public Automation(String pathFTL, String pathCONF, String nameCONF) throws DocumentException {
		SAXReader reader = new SAXReader();
		File folder = new File(pathCONF);
		File file = new File(folder, nameCONF);
		Document doc = reader.read(file);
		myConfiguration = new MyConfiguration(doc.getRootElement());
		System.out.println(myConfiguration.toString());
		
		freemarkerUtil = new FreemarkerUtil(pathFTL, myConfiguration.getEncoding());
	}
	
	/**
	 * 构造函数
	 * @param pathFTL 模板所在路径
	 * @param doc 配置XML对象
	 */
	public Automation(String pathFTL, Document doc) {
		myConfiguration = new MyConfiguration(doc.getRootElement());
		freemarkerUtil = new FreemarkerUtil(pathFTL, myConfiguration.getEncoding());
	}
	
	/**
	 * 生成相关java类，接口和mybatis的mapper.xml，以及spring的service.xml
	 */
	public void processEntities() throws IOException, TemplateException {
		freemarkerUtil.process(myConfiguration);
	}
	
	/**
	 * 生成公共配置文件，spring-applicationContent.xml,mybatis-applicationConfig.xml,datasource.properties
	 */
	public void processConfig() throws IOException, TemplateException {
		File file = new File(myConfiguration.getSource());
		Writer writer = new OutputStreamWriter(new FileOutputStream(new File(file, Global.CONFIG_SPRING)), myConfiguration.getEncoding());
		Template template = freemarkerUtil.getTemplate(Global.FTL_SPRING);
		Map<String, Object> springObjs = new HashMap<String, Object>();
		springObjs.put("encoding", myConfiguration.getEncoding());
		springObjs.put("confDS", Global.CONFIG_DS);
		springObjs.put("confMybatis", Global.CONFIG_MYBATIS);
		String mapperPath = myConfiguration.getPackageMapper().replace(".", File.separator);
		if (!StringUtils.isEmpty(mapperPath)) mapperPath += File.separator;
		springObjs.put("mapperPath", mapperPath + "*." + Global.CONFIG_MAPPER_SUF + ".xml");
		template.process(springObjs, writer);
		writer.close();
		
		writer = new OutputStreamWriter(new FileOutputStream(new File(file, Global.CONFIG_DS)), myConfiguration.getEncoding());
		template = freemarkerUtil.getTemplate(Global.FTL_DS);
		Map<String, Object> dsObjs = new HashMap<String, Object>();
		dsObjs.put("encoding", myConfiguration.getEncoding());
		dsObjs.put("ip", Global.DS_IP);
		dsObjs.put("port", Global.DS_PORT);
		dsObjs.put("dataBase", Global.DS_DATABASE);
		dsObjs.put("user", Global.DS_USER);
		dsObjs.put("password", Global.DS_PASSWORD);
		template.process(dsObjs, writer);
		writer.close();
		
		writer = new OutputStreamWriter(new FileOutputStream(new File(file, Global.CONFIG_MYBATIS)), myConfiguration.getEncoding());
		template = freemarkerUtil.getTemplate(Global.FTL_MYBATIS);
		Map<String, Object> mybatisObjs = new HashMap<String, Object>();
		mybatisObjs.put("encoding", myConfiguration.getEncoding());
		mybatisObjs.put("entities", myConfiguration.getEntities());
		mybatisObjs.put("packageEntity", myConfiguration.getPackageEntity());
		template.process(mybatisObjs, writer);
		writer.close();
		
		writer = new OutputStreamWriter(new FileOutputStream(new File(file, Global.CONFIG_WEB)), myConfiguration.getEncoding());
		template = freemarkerUtil.getTemplate(Global.FTL_WEB);
		Map<String, Object> webObjs = new HashMap<String, Object>();
		webObjs.put("encoding", myConfiguration.getEncoding());
		webObjs.put("name", Global.COMMON_PROJECT_NAME);
		webObjs.put("confSpring", Global.CONFIG_SPRING);
		String injectionPath = myConfiguration.getPackageInjection().replace(".", File.separator);
		if (!StringUtils.isEmpty(injectionPath)) injectionPath += File.separator;
		webObjs.put("injectionPath", injectionPath + "*." + Global.CONFIG_SERVICE_SUF + ".xml");
		template.process(webObjs, writer);
		writer.close();
	}
}
